package cn.hit.thread;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JProgressBar;
import javax.swing.JTextArea;

import cn.hit.constant.HitConstants;
import cn.hit.jdbc.DBAccessSington;
import cn.hit.utils.HitUtils;

public class ExecuteSqlThread extends Thread {

    private String targetPath;
    private JTextArea logArea;
    private JProgressBar pbar;
    private DBAccessSington dbAccess;
    private StringBuilder failPrintLog;
    private StringBuilder exportLog;

    public ExecuteSqlThread(String targetPath, JTextArea logArea,
            JProgressBar pbar) {
        this.dbAccess = DBAccessSington.getInstance();
        this.targetPath = targetPath;
        this.logArea = logArea;
        this.pbar = pbar;
        this.failPrintLog = new StringBuilder();
        this.exportLog = new StringBuilder();
    }

    /**
     * 执行文件里面的sql语句
     */
    @SuppressWarnings("static-access")
    @Override
    public void run() {
        List<File> files = new ArrayList<File>();
        HitUtils.loopFiles(files, targetPath + "\\" + HitConstants.EXPORT_SOURCE_DRICTORY);
        pbar.setMaximum(files.size());
        pbar.setVisible(true);

        String tmpStr = "";
        BufferedReader reader = null;
        StringBuilder sb;
        String path;
        logArea.setText("");
        int successCount = 0;
        int failCount = 0;
        for (File file : files) {
            sb = new StringBuilder();
            try {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),
                        HitConstants.FILE_ENCODING));
                while ((tmpStr = reader.readLine()) != null) {
                    sb.append(tmpStr+"\n");
                }
                reader.close();
            } catch (Exception e) {
                failPrintLog.append("执行失败脚本：" + file.getAbsolutePath() + "\n");
                failCount++;
                e.printStackTrace();
            }
            exportLog.append(file.getAbsolutePath());
            if (dbAccess.excuteSql(sb.toString(), exportLog) == 1) {
                successCount++;
            } else {
                failCount++;
                failPrintLog.append("执行失败脚本：" + file.getAbsolutePath() + "\n");
            }

            String printedText;
            if (successCount > HitConstants.LOG_ROLL_LINE_SIZE) {
                printedText = logArea.getText().substring(logArea.getText().indexOf("......") + 7,
                        logArea.getText().length());
            } else {
                printedText = logArea.getText();
            }
            path = file.getAbsolutePath();
            logArea.setText(printedText + "正在执行sql语句："
                    + path.substring(path.lastIndexOf("/") + 1, path.length()) + "......\n");
            pbar.setValue(successCount + failCount);
            try {
                this.sleep(HitConstants.DEFER_SLEEP_TIME);
            } catch (InterruptedException e) {
                logArea.setText("************************非正常结束************************");
                exportLog.append(logArea.getText());
                e.printStackTrace();
            }
        }
        if (successCount == 0) {
            logArea.setText("************************执行脚本列表路径非法************************");
            exportLog.append(logArea.getText());
        } else {
            failPrintLog.insert(0, "执行失败脚本数量:" + failCount + "\n");
            logArea.setText("************************执行脚本完成************************\n"
                    + "执行成功脚本数量:" + successCount + "\n" + failPrintLog.toString());
        }
        pbar.setVisible(false);
        HitUtils.exportLogFile(targetPath, "执行sql脚本日志", exportLog.toString());
    }

}
